package com.me.mygdxgame;

import java.util.List;
import java.util.Random;

import kryonetpackets.NetworkPlayer;
import client.GameClient;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.me.mygdxgame.login.LoginScreen;
import com.me.mygdxgame.menu.MainMenu;
import com.me.mygdxgame.planet.Planet;
import com.me.mygdxgame.player.Player;
import com.me.mygdxgame.player.PlayerGroup;

public class MyGdxGame implements ApplicationListener {
	private OrthographicCamera camera;
	private GameClient gameClient;
	
	private PlayerGroup thisPlayer;
	
	private MainMenu mainMenu;
	private LoginScreen loginScreen;
	private Planet currentPlanet = null;
	
	@Override
	public void create() {
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		
		this.camera = new OrthographicCamera(1, h/w);
		
		//Server connection code
		gameClient = new GameClient(this);
		gameClient.startClient();

		this.initLoginScreen();
		this.mainMenu = new MainMenu(w, h);
	}
	
	public void initLoginScreen() {
		if (this.loginScreen == null ) {
			this.loginScreen = new LoginScreen(this.camera, this);
		}
	}

	@Override
	public void dispose() {
		if (this.loginScreen != null) {
			this.loginScreen.dispose();
			this.loginScreen = null;
		}
		if (this.currentPlanet != null) {
			this.currentPlanet.dispose();
			this.currentPlanet = null;
		}
		this.mainMenu.dispose();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		
		if (this.loginScreen != null) {
			this.loginScreen.render(Gdx.graphics.getDeltaTime());
		}
		else {
			//TODO Change to the appropriate screen
			//this.mainMenu.render(Gdx.graphics.getDeltaTime());
			if (this.currentPlanet != null)
				this.currentPlanet.render(Gdx.graphics.getDeltaTime());
		}
	}

	@Override
	public void resize(int width, int height) {}

	@Override
	public void pause() {}

	@Override
	public void resume() {}
	
	/**
	 * A luxury call, so the various game screens can handle their own
	 * controller, instead of placing it all here.
	 * @return
	 */
	public final GameClient getGameClient() { return this.gameClient; }
	
	public void logIn(NetworkPlayer np) {
		this.thisPlayer = new PlayerGroup(true);
		this.thisPlayer.addPlayer(new Player(np));

		//TODO: Change this to loading based on server info
		this.currentPlanet = new Planet(this.thisPlayer,this.gameClient);
		
		this.loginScreen.dispose();
		this.loginScreen = null;
	}
	
	public void playerGroupJoinedPlanet(List<NetworkPlayer> networkPlayers) {
		if (this.currentPlanet != null) {
			
			//TODO move all position logic to the planet
			Random gen = new Random();
			gen.setSeed(System.currentTimeMillis());
			PlayerGroup pg = new PlayerGroup(false);
			pg.setPosition(gen.nextFloat() * (Gdx.graphics.getWidth() - 50f) + 25f,
					gen.nextFloat() * (Gdx.graphics.getHeight() - 50f) + 25);
			for (NetworkPlayer np : networkPlayers) {
				pg.addPlayer(new Player(np));
			}
			this.currentPlanet.addGroup(pg);
		} else {
			System.err.println("Server thinks player is on a planet it isn't.");
		}
	}
	public void playerGroupLeftPlanet(List<NetworkPlayer> networkPlayers) {
		if (this.currentPlanet != null) {
			PlayerGroup pg = new PlayerGroup(false);
			for (NetworkPlayer np : networkPlayers) {
				pg.addPlayer(new Player(np));
			}
			this.currentPlanet.removeGroup(pg);
		} else {
			System.err.println("Server thinks player is on a planet it isn't.");
		}
	}
	public void appendChatBox(String text)
	{
		this.currentPlanet.getChatBox().appendText(text);
	}
	public Player getThisPlayer()
	{
		return this.thisPlayer.getPlayers().get(0);
	}
}
